package co.fitstart.admin.service.impl;

import java.util.List;
import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.demo2do.core.persistence.GenericDaoSupport;
import com.demo2do.core.persistence.annotation.Dynamic;
import com.demo2do.core.support.Page;

import co.fitstart.admin.dao.CreditOrderDao;
import co.fitstart.admin.entity.search.CreditOrderSearch;
import co.fitstart.admin.service.CreditOrderService;
import co.fitstart.entity.credit.order.CreditOrder;
import co.fitstart.entity.credit.order.CreditOrderStatus;
import co.fitstart.entity.credit.product.CreditProductType;

/**
 * 
 * @author houtao
 *
 */
@Service("CreditOrderService")
public class CreditOrderServiceImpl extends DataServiceImpl<CreditOrder> implements CreditOrderService{

    @Autowired
    private GenericDaoSupport genericDaoSupport;
    
    @Dynamic
    private CreditOrderDao creditOrderDao;
    
	/*
	 * (non-Javadoc)
	 * @see co.fitstart.admin.service.CreditOrderService#list(co.fitstart.entity.credit.order.CreditOrderStatus, co.fitstart.entity.credit.product.CreditProductType, com.demo2do.core.support.Page)
	 */
	public List<CreditOrder> list(CreditOrderStatus status, CreditProductType type, Page page) {
		
		List<CreditOrder> result = creditOrderDao.listByPage(type, status, page.getBeginIndex(), page.getMaxResultRecords());
        page.calculate(result.size());
        
        return result.size() > page.getEveryPage() ? result.subList(0, page.getEveryPage()) : result;
	}

	/*
	 * (non-Javadoc)
	 * @see co.fitstart.admin.service.CreditOrderService#search(co.fitstart.admin.entity.search.CreditOrderSearch, com.demo2do.core.support.Page)
	 */
	@SuppressWarnings("unchecked")
	public List<CreditOrder> search(CreditOrderSearch orderSearch, Page page) {
		String s = orderSearch.buildSentenceTemplate();
        Map<String, Object> parameters = orderSearch.buildParameters();
        List<CreditOrder> result = genericDaoSupport.searchForList(s, parameters, page.getBeginIndex(), page.getMaxResultRecords());
        page.calculate(result.size());
        return result.size() > page.getEveryPage() ? result.subList(0, page.getEveryPage()) : result;
	}

	/*
	 * (non-Javadoc)
	 * @see co.fitstart.admin.service.CreditOrderService#totalOrder(co.fitstart.admin.entity.search.CreditOrderSearch)
	 */
	public int totalOrder(CreditOrderSearch orderSearch) {
		return genericDaoSupport.count(orderSearch.buildSentenceTemplate(), orderSearch.buildParameters());
	}

	/*
	 * (non-Javadoc)
	 * @see co.fitstart.admin.service.CreditOrderService#editAddress(co.fitstart.entity.credit.order.CreditOrder)
	 */
	public void edit(CreditOrder order) {
		genericDaoSupport.update(order);
	}

}
